package com.zbkj.admin.task.monitor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zbkj.common.model.merchant.Merchant;
import com.zbkj.common.model.monitor.ZfbMonitorEquipment;
import com.zbkj.service.service.MerchantService;
import com.zbkj.service.service.monitor.IZfbMonitorEquipmentService;
import com.zbkj.service.service.monitor.ZfbMonitorDeviceStatusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 监控设备定时处理任务
 * +----------------------------------------------------------------------
 * | 功能描述：定时检查监控设备状态，根据过期情况自动开启或关闭监控
 * +----------------------------------------------------------------------
 * @author xiaolic
 * +----------------------------------------------------------------------
 * @date 2025-05-07
 */
@Slf4j
@Component("MonitorProcessTask")
public class MonitorProcessTask {

    @Autowired
    private MerchantService merchantService;

    @Autowired
    private IZfbMonitorEquipmentService zfbMonitorEquipmentService;

    @Autowired
    private ZfbMonitorDeviceStatusService zfbMonitorDeviceStatusService;

    /**
     * 每小时处理一次所有监控设备的状态
     * 执行逻辑：
     * 1. 获取所有商户信息
     * 2. 遍历各商户下的所有监控设备
     * 3. 检查设备过期时间，并根据状态开启或关闭摄像头
     */
    public void processAllMonitorDevices() {
        log.info("开始执行监控设备状态处理定时任务...");

        try {
            // 1. 获取所有商户
            List<Merchant> merchantList = merchantService.list();
            log.info("共获取到{}个商户", merchantList.size());

            int totalDevices = 0;
            int processedDevices = 0;

            // 2. 遍历所有商户
            for (Merchant merchant : merchantList) {
                // 获取商户下的所有监控设备
                LambdaQueryWrapper<ZfbMonitorEquipment> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(ZfbMonitorEquipment::getMerchantId, merchant.getId());
                List<ZfbMonitorEquipment> equipmentList = zfbMonitorEquipmentService.list(queryWrapper);

                if (CollectionUtils.isEmpty(equipmentList)) {
                    log.info("商户[{}]没有监控设备", merchant.getName());
                    continue;
                }

                log.info("商户[{}]共有{}个监控设备", merchant.getName(), equipmentList.size());
                totalDevices += equipmentList.size();

                // 3. 处理每个设备
                for (ZfbMonitorEquipment equipment : equipmentList) {
                    try {
                        // 获取设备过期信息
                        Map<String, Object> expireInfo = zfbMonitorDeviceStatusService.getExpireTimeByLicense(equipment.getLicense());

                        // 处理设备状态
                        boolean result = zfbMonitorDeviceStatusService.processDeviceStatus(equipment.getLicense(), expireInfo, null);
                        if (result) {
                            processedDevices++;
                        }
                    } catch (Exception e) {
                        log.error("处理设备[{}]异常", equipment.getLicense(), e);
                    }
                }
            }

            log.info("监控设备状态处理定时任务完成，共处理{}个设备，成功处理{}个", totalDevices, processedDevices);
        } catch (Exception e) {
            log.error("监控设备状态处理定时任务执行异常", e);
        }
    }
}
